home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.4 / Migrating / ASLExample / GetFileName.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  5.1 KB  |  209 lines

  1. ;/* GetFileName.c - Execute me to compile me with SAS/C v5.10a
  2. LC -b1 -cfistq -v -y -j73 getfilename.c
  3. Blink FROM LIB:c.o,getfilename.o TO getfilename LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. This simple example provides a subroutine getfilename(buffer,win,mode) which
  7. gets a filename using the ASL file requester if asl.library is available.
  8. Otherwise it calls oldgetfilename(buffer,win,mode) which could call your old
  9. file request code (here it just prompts for a filename from stdin).
  10.  
  11. Mode is 0 (MODE_LOAD) for a load, and 1 (MODE_SAVE) for a save.
  12.  
  13. If a window pointer is provided, the requester will come up on that window's
  14. screen.  This example first opens a load requester, then a save requester.
  15. A custom title string and OK gadget string are used.
  16. */
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20. #include <dos/dos.h>
  21. #include <intuition/intuition.h>
  22. #include <libraries/asl.h>
  23.  
  24. #ifdef LATTICE
  25. #include <clib/exec_protos.h>
  26. #include <clib/dos_protos.h>
  27. #include <clib/asl_protos.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32.  
  33. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  34. int chkabort(void) { return(0); }  /* really */
  35. #endif
  36.  
  37.  
  38. /**********    debug macros     ***********/
  39. #define MYDEBUG     0    /* Set to 1 for debugging output. */
  40. void kprintf(UBYTE *fmt,...);
  41. void dprintf(UBYTE *fmt,...);
  42. #define DEBTIME 0
  43. #define bug printf
  44. #if MYDEBUG
  45. #define D(x) (x); if(DEBTIME>0) Delay((ULONG)DEBTIME);
  46. #else
  47. #define D(x) ;
  48. #endif /* MYDEBUG */
  49. /********** end of debug macros **********/
  50.  
  51.  
  52. #define MINARGS 1
  53. #define ANY_LIB_VER 0L
  54.  
  55. UBYTE *vers = "$VER: getfilename 37.3 (07.08.91)";
  56. UBYTE *Copyright = 
  57.   "getfilename v37.3\nCopyright (c) 1990-91 Commodore-Amiga, Inc.  All Rights Reserved";
  58. UBYTE *usage = "Usage: getfilename";
  59.  
  60. /* protos */
  61. UBYTE  *getfilename(UBYTE *copybuf, struct Window *win, int mode);
  62. UBYTE  *oldgetfilename(UBYTE *copybuf, struct Window *win, int mode);
  63. BOOL   AslRequestTags( APTR request, ULONG tags, ...);
  64. void bye(UBYTE *s, int e);
  65. void cleanup(void);
  66.  
  67. /* for file requester */
  68. #define MODE_LOAD 0
  69. #define MODE_SAVE 1
  70. #define NBUFSZ (256)
  71. UBYTE fnamebuf[NBUFSZ] = {0};
  72. #define INITDIR "SYS:"
  73.  
  74.  
  75. struct Library    *AslBase = NULL;
  76. struct FileRequester *freq = NULL;
  77.  
  78. BOOL FromWb;
  79.  
  80. /*
  81.  *  MAIN
  82.  */
  83. void main(int argc, char **argv)
  84.     {
  85.     UBYTE *filename;
  86.  
  87.     FromWb = (argc==0) ? TRUE : FALSE;
  88.  
  89.     if((argc)&&(argc<MINARGS))
  90.     {
  91.     printf("%s\n%s\n",Copyright,usage);
  92.     bye("",RETURN_OK);
  93.     }
  94.  
  95.  
  96.     AslBase = OpenLibrary("asl.library", ANY_LIB_VER); /* used if available */
  97.  
  98.     /* if on a custom screen, we'd pass window as second arg */
  99.  
  100.     printf("\nHere is a load requester.\n");
  101.     printf("When requester appears, go to any volume:dir and select a file\n");
  102.     Delay(1L * TICKS_PER_SECOND);
  103.  
  104.     if(filename = getfilename(fnamebuf,NULL,MODE_LOAD))
  105.     {
  106.     printf("filename for load is %s\n",fnamebuf);
  107.     }
  108.     else printf("No filename selected for load.\n");
  109.  
  110.  
  111.     printf("\nHere is a save requester (allows directory creation).\n");
  112.     printf("\nNote that the previous directory and file are remembered.\n");
  113.     Delay(1L * TICKS_PER_SECOND);
  114.  
  115.     if(filename = getfilename(fnamebuf,NULL,MODE_SAVE))
  116.     {
  117.     printf("filename for save is %s\n",fnamebuf);
  118.     }
  119.     else printf("No filename selected for save.\n");
  120.  
  121.     bye("",RETURN_OK);
  122.     }
  123.  
  124.  
  125. void bye(UBYTE *s, int e)
  126.    {
  127.    if(*s)
  128.     {
  129.     printf("%s\n",s);
  130.     if(FromWb)
  131.         {
  132.         printf("Press <RET> to exit: ");
  133.         getchar();
  134.         }
  135.     }
  136.    cleanup();
  137.    exit(e);
  138.    }
  139.  
  140.  
  141. void cleanup()
  142.    {
  143.    if (freq)        FreeAslRequest(freq);
  144.    if (AslBase)        CloseLibrary(AslBase);
  145.    }
  146.  
  147.  
  148. UBYTE *getfilename(UBYTE *copybuf, struct Window *win, int mode)
  149. {
  150. ULONG funcflags;
  151. UBYTE *hail, *oktext;
  152.  
  153. hail   = (mode == MODE_LOAD) ? "Load File" : "Save File";
  154. oktext = (mode == MODE_LOAD) ? "Load" : "Save";
  155. funcflags = (mode == MODE_SAVE ) ? FILF_SAVE : 0;
  156. funcflags |= ((win)&&(win->UserPort)) ? 0 : FILF_NEWIDCMP;
  157.  
  158.  
  159. D(bug("In getfilename\n"));
  160.  
  161. /* If have asl.library, try requester */
  162. if(AslBase)
  163.     {
  164.     /* if we don't have one, alloc it */
  165.     if(!freq) freq = AllocAslRequest(ASL_FileRequest, NULL);
  166.  
  167.     if(freq)
  168.     {
  169.     D(bug("About to Request File\n"));
  170.  
  171.     if(AslRequestTags((APTR)freq,
  172.                 ASL_Hail,hail,
  173.                 ASL_OKText,oktext,
  174.                 ASL_Window, win,
  175.                 ASL_FuncFlags, funcflags,
  176.                 TAG_DONE ))
  177.         {
  178.         D(bug("Request File successful\n"));
  179.         strcpy(copybuf,freq->rf_Dir);
  180.         if(AddPart(copybuf,freq->rf_File,NBUFSZ)) return(copybuf);
  181.         }
  182.     else return(NULL);
  183.     }
  184.     }
  185. else return(oldgetfilename(copybuf,win,mode));
  186. }
  187.  
  188.  
  189. /* Replace this with code that calls your
  190.  * old file requester or brings up a string gadget, etc.
  191.  */
  192. UBYTE *oldgetfilename(UBYTE *copybuf, struct Window *win, int mode)
  193. {
  194.     printf("\nEnter filename, or <Return> to exit: ");
  195.     copybuf[0] = '\0';
  196.     gets(copybuf);
  197.     return(copybuf[0] ? copybuf : NULL);
  198. }
  199.  
  200. /*------------------------------------------------------------------------*/
  201.  
  202.  
  203. BOOL AslRequestTags(request, tags)
  204. APTR request;
  205. ULONG tags;
  206.     {
  207.     return(AslRequest(request, (struct TagItem *)&tags));
  208.     }
  209.